var webpack = require('webpack');
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var UglifyJSPlugin = require('uglifyjs-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');

var __entry = {
    'chivox': __dirname + '/src/chivoxsdk',
    'examples/js/index': __dirname + '/examples/js/index',
    'examples/js/flashdetect': __dirname + '/examples/js/flashdetect',
    'examples/js/statuscode': __dirname + '/examples/js/statuscode',
    'examples/js/aiplayer_tts': __dirname + '/examples/js/tts',
    'examples/js/aipanel_enword': __dirname + '/examples/js/aipanel_enword',
    'examples/js/aipanel_ensent': __dirname + '/examples/js/aipanel_ensent',
    'examples/js/aipanel_enrec': __dirname + '/examples/js/aipanel_enrec',
    'examples/js/aipanel_cnword': __dirname + '/examples/js/aipanel_cnword',
    'examples/js/aipanel_cnsent': __dirname + '/examples/js/aipanel_cnsent',
    'examples/js/aipanel_multiple': __dirname + '/examples/js/aipanel_multiple',
    'examples/js/aipanel_paragraph': __dirname + '/examples/js/aipanel_paragraph',
    'examples/js/aiplayer_basic': __dirname + '/examples/js/aiplayer_basic',
    'examples/js/airecorder_basic': __dirname + '/examples/js/airecorder_basic',
    'examples/js/exam_oesy': __dirname + '/examples/js/exam_oesy',
    'examples/js/exam_pred': __dirname + '/examples/js/exam_pred',
    'examples/js/exam_pict': __dirname + '/examples/js/exam_pict',
    'examples/js/exam_q3a5': __dirname + '/examples/js/exam_q3a5',
    'examples/js/exam_scne': __dirname + '/examples/js/exam_scne',
    'examples/js/exam_story': __dirname + '/examples/js/exam_story',
};

var __plugins = [
    new HtmlWebpackPlugin({
        title: 'ChiVox API JSSDK Example',
        filename: "examples/index.html",
        chunks: ['chivox', 'examples/js/index'],
        template: __dirname + '/examples/index.html'
    }),
    new HtmlWebpackPlugin({
        title: '检测Flash版本',
        filename: "examples/aiflashdetect.html",
        chunks: ['chivox', 'examples/js/flashdetect'],
        template: __dirname + '/examples/aiflashdetect.html'
    }),
    new HtmlWebpackPlugin({
        title: '状态码查询',
        filename: "examples/aistatuscode.html",
        chunks: ['chivox', 'examples/js/statuscode'],
        template: __dirname + '/examples/aistatuscode.html'
    }),
    new HtmlWebpackPlugin({
        title: '使用AiPlayer搭建中英文合成应用(v2.0)',
        filename: "examples/aiplayer_tts.html",
        chunks: ['chivox', 'examples/js/aiplayer_tts'],
        template: __dirname + '/examples/aiplayer_tts.html'
    }),
    new HtmlWebpackPlugin({
        title: '使用AiPanel搭建英文词语评分应用',
        filename: "examples/aipanel_enword.html",
        chunks: ['chivox', 'examples/js/aipanel_enword'],
        template: __dirname + '/examples/aipanel_enword.html'
    }),
    new HtmlWebpackPlugin({
        title: '使用AiPanel搭建英文句子评分应用',
        filename: "examples/aipanel_ensent.html",
        chunks: ['chivox', 'examples/js/aipanel_ensent'],
        template: __dirname + '/examples/aipanel_ensent.html'
    }),
    new HtmlWebpackPlugin({
        title: '使用AiPanel搭建英文识别应用',
        filename: "examples/aipanel_enrec.html",
        chunks: ['chivox', 'examples/js/aipanel_enrec'],
        template: __dirname + '/examples/aipanel_enrec.html'
    }),
    new HtmlWebpackPlugin({
        title: '使用AiPanel搭建中文词语评分应用',
        filename: "examples/aipanel_cnword.html",
        chunks: ['chivox', 'examples/js/aipanel_cnword'],
        template: __dirname + '/examples/aipanel_cnword.html'
    }),
    new HtmlWebpackPlugin({
        title: '使用AiPanel搭建中文句子评分应用',
        filename: "examples/aipanel_cnsent.html",
        chunks: ['chivox', 'examples/js/aipanel_cnsent'],
        template: __dirname + '/examples/aipanel_cnsent.html'
    }),
    new HtmlWebpackPlugin({
        title: '页面中含有多个播放录音按钮',
        filename: "examples/aipanel_multiple.html",
        chunks: ['chivox', 'examples/js/aipanel_multiple'],
        template: __dirname + '/examples/aipanel_multiple.html'
    }),
    new HtmlWebpackPlugin({
        title: '段落评分',
        filename: "examples/aipanel_paragraph.html",
        chunks: ['chivox', 'examples/js/aipanel_paragraph'],
        template: __dirname + '/examples/aipanel_paragraph.html'
    }),
    new HtmlWebpackPlugin({
        title: 'AiRecoder基本用法',
        filename: "examples/airecorder_basic.html",
        chunks: ['chivox', 'examples/js/airecorder_basic'],
        template: __dirname + '/examples/airecorder_basic.html'
    }),
    new HtmlWebpackPlugin({
        title: 'AiPlayer基本用法',
        filename: "examples/aiplayer_basic.html",
        chunks: ['chivox', 'examples/js/aiplayer_basic'],
        template: __dirname + '/examples/aiplayer_basic.html'
    }),
    new HtmlWebpackPlugin({
        title: '口头作文',
        filename: "examples/exam_oesy.html",
        chunks: ['chivox', 'examples/js/exam_oesy'],
        template: __dirname + '/examples/exam_oesy.html'
    }),
    new HtmlWebpackPlugin({
        title: '短文朗读',
        filename: "examples/exam_pred.html",
        chunks: ['chivox', 'examples/js/exam_pred'],
        template: __dirname + '/examples/exam_pred.html'
    }),
    new HtmlWebpackPlugin({
        title: '看图说话',
        filename: "examples/exam_pict.html",
        chunks: ['chivox', 'examples/js/exam_pict'],
        template: __dirname + '/examples/exam_pict.html'
    }),
    new HtmlWebpackPlugin({
        title: '三问五答',
        filename: "examples/exam_q3a5.html",
        chunks: ['chivox', 'examples/js/exam_q3a5'],
        template: __dirname + '/examples/exam_q3a5.html'
    }),
    new HtmlWebpackPlugin({
        title: '情景对话',
        filename: "examples/exam_scne.html",
        chunks: ['chivox', 'examples/js/exam_scne'],
        template: __dirname + '/examples/exam_scne.html'
    }),
    new HtmlWebpackPlugin({
        title: '故事复述',
        filename: "examples/exam_story.html",
        chunks: ['chivox', 'examples/js/exam_story'],
        template: __dirname + '/examples/exam_story.html'
    }),
    new webpack.LoaderOptionsPlugin({
        options: {
            worker: {
                output: {
                    filename: "worker/recordWorker.[hash:8].js",
                    chunkFilename: "recordWorker.js"
                }
            },
        }
    }),
    new ExtractTextPlugin({
        filename: (getPath) => {
            return getPath('css/[name].[hash:8].css').replace('css/examples/js', 'examples/css')
        }
    }),
];

var __rules = [{
        test: /\.json$/,
        loader: 'json-loader'
    },
    {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
            fallback: 'style-loader',
            use: 'css-loader'
        })
    },
    {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
            presets: ['es2015']
        }
    }
];

var __dist_js;
if (process.env.ENV == 'prod') {
    __dist_js = '[name].[hash:8].min.js';

    __plugins = __plugins.concat([
        new UglifyJSPlugin({
            ie8: true,
            exclude: /\/examples/,
            sourceMap: false,
            mangle: false,
            compress: {
                warnings: false
            },
            except: ['$super', '$', 'exports', 'require']
        }),
        new CopyWebpackPlugin([{
            from: path.resolve(__dirname + '/static'),
            to: path.resolve(__dirname + '/dist/static'),
            force: true,
            toType: 'dir',
            ignore: ['.*']
        }]),
    ]);

    __rules.push({
        test: /\.(png|jpg)$/,
        loader: 'url-loader?limit=8192&name=images/[name].[hash:8].[ext]'
    });
} else {
    __dist_js = '[name].js';
    __plugins.push(new webpack.HotModuleReplacementPlugin());
    __rules.push({
        test: /\.(png|jpg)$/,
        loader: 'url-loader'
    });
}

var __config = {
    devtool: 'eval-source-map',
    entry: __entry,
    output: {
        path: __dirname + '/dist',
        filename: __dist_js,
        libraryTarget: "umd",
        library: "chivox"
    },
    module: {
        rules: __rules
    },
    plugins: __plugins,
    externals: {
        jquery: 'jQuery',
        chivox: 'ChiVoxSDK'
    }
};

if (process.env.ENV != 'prod') {
    __config['devServer'] = {
        historyApiFallback: true,
        inline: true,
        hot: true,
        setup: function(app) {
            var sha1 = require("sha1");

            var handle_token = function(req, res) {
                let key = '14255202120000cf';
                let secret = '988aaf63008b6db30f38bd60be0c3534';
                let t = new Date().getTime().toString();

                res.json({
                    timestamp: t,
                    sig: sha1(key + t + secret)
                });
            };

            app.get('/token', handle_token);
            app.post('/token', handle_token);
        }
    };
}

module.exports = __config
